<?php

namespace app\console\controller;

use app\common\controller\Console;
use think\Exception;
use think\Log;
use think\Request;

class Sku extends Console
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = [];
        $status = request()->get('status', false);
        if (false !== $status) {
            $where['status']=$status;
        }
        $model = model('Activity');
        $list = $model->where($where)->order('active_id desc')->paginate(10, false, ['query' => request()->get()]);
        $counts = [
            'all' => $model->count(),
            'waiting' => $model->where('status', 0)->count(),
            'going' => $model->where('status', 1)->count(),
            'finish' => $model->where('status', 2)->count(),
            'cancel' => $model->where('status', 3)->count(),
        ];
        $this->assign([
            'list' => $list,
            'count' => $counts,
        ]);
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function insert(Request $request)
    {
        if ($data = $request->post()) {
            $data['add_time'] = $data['modified'] = time();
            try {
                model('Activity')->save($data);
            } catch (Exception $e) {
                Log::record($e->getMessage());
            }
            $this->success('添加成功', url('index'));
        } else {
            abort(404, '错误请求');
        }
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $info = model('Activity')->where('active_id', $id)->find();
        if (!$info) {
            abort(404, '错误请求');
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if ($data = $request->post()) {
            unset($data['id']);
            try {
                model('Activity')->save($data, ['active_id' => $id]);
            } catch (Exception $e) {
                Log::record($e->getMessage());
            }
            $this->success('更新成功');
        } else {
            abort(404, '请求错误');
        }

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            model('Activity')->where('active_id', $id)->delete();
        } catch (Exception $e) {
            Log::record($e->getMessage());
        }
        $this->success('删除成功', url('index'), '', 1);
    }
    
    public function cancel($id)
    {
        try {
            model('Activity')->save(['status' => 3], ['active_id' => $id]);
        } catch (Exception $e) {
            Log::record($e->getMessage());
        }
        $this->success('操作成功', url('index'), '', 0);
    }
}
